package com.bigData.main.controller;

import com.bigData.main.mapper.CarMapper;
import com.bigData.main.pojo.Car;
import com.bigData.main.service.CarService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class CarController {
    @Resource
    CarService cars;
    @Resource
    private CarMapper carMapper;

    // 保持原有插入单个车辆的功能
    @RequestMapping("/insertacar")
    public Integer insertaCar(@RequestBody Car car) {

        // 插入车名、颜色、价格、状态必须填写
        if (car.getCname() == null || car.getColor() == null ||
                car.getPrice() <= 0 || (car.getStatus() == null)) {
            throw new IllegalArgumentException("车名、颜色、价格和状态必须填写，状态应为0或1");
        }
        return cars.insertacar(car);
    }

    // 批量插入车辆
    @PostMapping("/insertcarall")
    public String batchInsertCars(@RequestBody List<Car> carsList) {
        if (carsList.isEmpty()) {
            return "文件内容无效，未能解析到任何有效记录。";
        }

        int insertedCount = cars.batchInsertCars(carsList);
        return "批量插入成功，共插入 " + insertedCount + " 条记录!";
    }

    // 保持原有查询所有车辆的功能
    @RequestMapping("/selectcarall")
    public List<Car> selectCar() {
        return cars.selectAllCar(new Car());
    }

    @RequestMapping("/selectacar")
    public List<Car> selectaCar(@RequestParam("cid") int cid) {
        System.out.println("查询车辆 ID: " + cid);
        List<Car> carsList = cars.selectacar(cid);
        System.out.println("查询结果: " + carsList);
        return carsList;
    }

    // 查询已售出的车辆
    @RequestMapping("/sselectcarall")
    public List<Car> selectSoldCars() {
        return cars.selectSoldCars();
    }

    // 查询未售出的车辆
    @RequestMapping("/unselectcarall")
    public List<Car> selectUnsoldCars() {
        return cars.selectUnsoldCars();
    }

    // 用户更新车辆信息
    @RequestMapping("/updatecar")
    public String updateCar(@RequestBody Car car) {
        if (car.getCname() == null || car.getColor() == null || car.getPrice() == 0 || car.getStatus() == null) {
            throw new IllegalArgumentException("车名、颜色、价格和状态必须填写，状态应为0或1");
        } else {
            cars.updatecar(car);
            return "更新成功，车名：" + car.getCname();
        }
    }

    @RequestMapping("/updatecarstatus")
    public String updateCarstatus(int cid) {
        cars.updatecarstatus(cid);
        return "账单号为" + cid + "出库成功";
    }

    @PostMapping("/insertCarStatus")
    @ResponseBody
    public Map<String, Object> insertCarStatus(@RequestParam("cid") int cid) {
        try {
            // 调用插入方法
            int result = cars.updatecarstatus(cid);
            if (result > 0) {
                return createSuccessResult();
            } else {
                return createFailureResult("插入失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return createFailureResult("服务器内部错误: " + e.getMessage());
        }
    }

    private Map<String, Object> createSuccessResult() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "插入成功");
        return result;
    }

    private Map<String, Object> createFailureResult(String message) {
        Map<String, Object> result = new HashMap<>();
        result.put("success", false);
        result.put("message", message);
        return result;
    }

    // 用户删除车辆信息
    @RequestMapping(value = "/deletecar", method = RequestMethod.DELETE)
    public String deleteCar(@RequestParam int cid) {
        // 执行删除操作
        int result = cars.deletecar(cid);
        if (result > 0) {
            return "用户删除成功，车辆ID：" + cid;
        } else {
            throw new IllegalArgumentException("删除失败，未找到对应车辆ID：" + cid);
        }
    }

    @RequestMapping("/countCreator")
    public Integer countCreator(String creator) {
        return cars.countCreator(creator);
    }

    @RequestMapping("/allcar")
    public Integer allCar() {
        return cars.allCar();
    }

    @RequestMapping("/alluCar")
    public Integer alluCar() {
        return cars.alluCar();
    }

    @RequestMapping("/allprice")
    public Integer allPrice() {
        return cars.allPrice();
    }

    @RequestMapping("timecount")
    public Integer timeCount() {
        return cars.timeCount();
    }


}